<template>
	<view class="box u-skeleton">
		<view class="topBg">
		</view>
		<view class="nav">
			<u-navbar :is-back='false' title="" :background="style" :border-bottom="false">
				<!-- #ifdef APP-PLUS -->
				<view class="control jsbc">
					<view class="slot-left">
						<span>聚辉佳食品</span>
					</view>
					<view class="slot-right ml2" @click="goSearch()">
						<view class="search flc">
							<u-icon class="ml2" name="/static/home/ss.png" size="26"></u-icon>
							<span class="ml2">请输入关键词</span>
						</view>
					</view>
				</view>
				<!-- #endif -->
				<!-- #ifdef MP-WEIXIN -->
				<view class="control flc">
					<view class="slot-left">
						<span>聚辉佳食品</span>
					</view>
					<view class="slot-right ml2" @click="goSearch()">
						<view class="search flc">
							<u-icon class="ml2" name="/static/home/ss.png" size="26"></u-icon>
							<span class="ml2">请输入关键词</span>
						</view>
					</view>
				</view>
				<!-- #endif -->
			</u-navbar>
		</view>
		<view class="wrapBox u-skeleton-fillet">
			<view class="wrap">
				<u-swiper :list="list" mode="rect" height="280"></u-swiper>
			</view>
		</view>
		<view class="H30">
		</view>
		<view class="container">
			<view class="menuList jsbw container_br u-skeleton-fillet" v-if="menuList">
				<view class="item " v-for="(item,index) in menuList" :key="index" @click="goTabs(item)">
					<view class="icon">
						<u-icon :name="item.image_format" size="90"></u-icon>
					</view>
					<view class="name">
						{{item.title}}
					</view>
				</view>
			</view>
		</view>
		<view class="settle u-skeleton-rect">
			<u-image width="100%" height="100%" src="/static/home/settle.png" :lazy-load="true"></u-image>
		</view>

		<!-- 商品标题 -->
		<view class="goods_title">
			<u-section title="热门推荐" line-color="#f47648" sub-title="查看更多" @click="goMore"></u-section>
		</view>
		<!-- 商品数据 -->
		<view class="goodsList" v-if="goodList.length>0">
			<u-waterfall v-model="flowList" ref="uWaterfall1">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index" @click="goDedail(item.id)">
						<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
						<u-lazy-load v-if="item.images_format" threshold="-450" border-radius="10" :image="item.images_format[0]"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.name}}
						</view>
						<view class="add_price jsbc">
							<view class="demo-price">
								￥{{item.min_price||item.price}}元
							</view>
							<view class="icon">
								<u-icon :name="'/static/home/jrgwc.png'" size="60"></u-icon>
							</view>
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index" @click="goDedail(item.id)">
						<u-lazy-load v-if="item.images_format" threshold="-450" border-radius="10" :image="item.images_format[0]"
							:index="index"></u-lazy-load>
						<view class="demo-title">
							{{item.name}}
						</view>
						<view class="add_price jsbc">
							<view class="demo-price">
								￥{{item.min_price||item.price}}元
							</view>
							<view class="icon">
								<u-icon :name="'/static/home/jrgwc.png'" size="60"></u-icon>
							</view>
						</view>
						
					</view>
				</template>
			</u-waterfall>
			<u-empty text="没有数据呢~" mode="data" v-if="flowList.length==0"></u-empty>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="addRandomData"></u-loadmore>
		</view>
		<u-skeleton :loading="loading" el-color="#ddd" border-radius="20" :animation="true" bgColor="#FFF"></u-skeleton>
	</view>
</template>
<script>
	import {
		closeload,
		tiao,
		toastload
	} from '@/utils/common.js'
	import baseUrl from '@/config/config.js'
	import { getSwiper,getNavigation,getGoodsList } from '@/api/index.js'
	export default {
		data() {
			return {
				keyword: "",
				style: {},
				list: [
				],
				menuList: [
				],
				loading: true,
				loadStatus: 'loadmore',
				flowList: [],
				page: 1,
				goodList: [
				]
			}
		},

		onLoad(res) {
			this.init()
			
		},
		onPageScroll(e) {
			if (e.scrollTop > 50) {
				this.style = {
					background: '#f47648'
				}
			} else {
				this.style = {
					background: 'transparent'
				}
			}
		},
		
		onReachBottom() {
			// 模拟数据加载
			if(this.loadStatus == 'loadmore'){
				this.loadStatus = 'loading';
				this.getData()
			}
		},
		methods: {
			init() {
				getSwiper().then(res=>{
					this.list = res.data.map(item=>{
						return {
							image:item.image_format,
							title:item.title
						}
					})
				})
				getNavigation().then(res=>{
					this.menuList = res.data
				})
				this.getData()
				this.addRandomData();
			},
			goPage(url) {
				tiao(url)
			},
			addRandomData() {
				for (let i = 0; i < this.goodList.length; i++) {
					// 先转成字符串再转成对象，避免数组对象引用导致数据混乱
					let item = JSON.parse(JSON.stringify(this.goodList[i]))
					this.flowList.push(item);
				}
				this.loading = false
			},
			goDedail(id){
				uni.navigateTo({
					url:'/page/goodsDetail/goodsDetail?id='+id
				})
				// tiao('/page/goodsDetail/goodsDetail?id='+item.id)
			},
			goSearch(){
				uni.navigateTo({
					url:'/page/search/search'
				})
			},
			getData(){
				getGoodsList({
					page:this.page
				}).then(res=>{
					console.log("商品数据");
					if(res.last_page>res.page){
						this.page++
						this.loadStatus = 'loadmore'
					}else{
						this.loadStatus = 'nomore'
					}
					if(res.list.length>0){
						this.goodList  = res.list
						this.addRandomData()
					}
				})
			},
			goTabs(item){
				console.log(JSON.parse(item.url));
				let url = JSON.parse(item.url)
				if(url.is_open){
					tiao(url.param.path,{...url.param.query})
				}else{
					let obj = {
						...url.param.query
					}
					uni.navigateTo({
						url:url.param.path + "?obj="+obj
					})
				}
			},
			goMore(){
				uni.navigateTo({
					url:'/page/goodsList/goodsList?type=1'
				})
			}
			
		}
	}
</script>
<style lang="scss" scoped>
	.topBg {
		width: 750rpx;
		height: 300rpx;
		background-image: url('/static/home/group.png');
		background-repeat: no-repeat;
		background-size: cover;
		position: absolute;
		top: 0;
		left: 0;
	}

	.control {
		width: 750rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
	}

	.slot-left {
		font-weight: 500;
		font-size: 36rpx;
		color: #FFFFFF;
	}

	.slot-right {
		.search {
			width: 360rpx;
			height: 64rpx;
			border-radius: 32rpx;
			background-color: rgba(255, 255, 255, 0.3);
			color: #FFFFFF;
		}
	}

	.wrap {
		width: 710rpx;
		height: 280rpx;
		z-index: 999;
	}

	.wrapBox {
		width: 710rpx;
		height: 280rpx;
		margin: 10rpx auto;
		box-sizing: border-box;
	}

	.menuList {
		.item {
			width: 25%;
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 10rpx;
		}
	}

	.settle {
		width: 710rpx;
		height: 200rpx;
		margin: 20rpx auto;
	}

	.goods_title {
		width: 750rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
	}

	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}
</style>